﻿using System;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;

namespace DocFactoryWinLibrary
{
   public class XmlDocumentConverter
   {

       public static void OpenDocument(string BaseDIRPath, string XmlDocFileName)
       {
           object missing = System.Reflection.Missing.Value;
           object objTrue = true;

           Word.ApplicationClass WordApp = new Word.ApplicationClass();

           try
           {

               object newTemplate = false;
               object docType = 0;
               object isVisible = true;
               object readOnly = 0;

               object strTmpSavePath = BaseDIRPath + XmlDocFileName;

               Word.Document aDoc = WordApp.Documents.Open(ref strTmpSavePath, ref missing, ref readOnly, ref missing, ref missing, ref missing,
               ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

               WordApp.Visible = true;
               aDoc.Activate();
           }
           catch (Exception)
           {

               throw;
           }
           finally
           {

           }

       }

       public static void ConvertXMLToDoc(string BaseDIRPath, string XmlDocFilename, string DocFileName, bool LeaveWordOpen)
       {
           
           if (!BaseDIRPath.EndsWith(@"\"));
             BaseDIRPath += @"\";

           object missing = System.Reflection.Missing.Value;
           object objTrue = true; 
           
           Word.ApplicationClass WordApp = new Word.ApplicationClass();

           try
           {

               // Set up all the parameters as generic objects so we can pass them in Documents.Add

               object newTemplate = false;
               object docType = 0;
               object isVisible = true;
               object readOnly = 0;

               object strTmpSavePath = BaseDIRPath + XmlDocFilename;
               object strSavePath = BaseDIRPath + DocFileName;

               Word.Document aDoc = WordApp.Documents.Open(ref strTmpSavePath, ref missing, ref readOnly, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);


               // Display the document to the user
               if (LeaveWordOpen)
               {
                   WordApp.Visible = true;
                   aDoc.Activate();
               }

               // Save the generated File
               aDoc.SaveAs(ref strSavePath, ref missing, ref readOnly, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

           }
           catch (Exception ExGeneral)
           {           
               throw ExGeneral;
           }
           finally
           {
               if (!LeaveWordOpen)
                   WordApp.Quit(ref objTrue, ref missing, ref missing);
           }

       }
   }
}
